<?php defined('SYSPATH') or die('No direct script access.');

class Model_Tag extends Model_Filter
{   
    const TAGS_PER_PAGE = 50;
    
    public static function get_tags_per_page()
    {
        return self::TAGS_PER_PAGE;
    }
   
    public function get_tags_by_category($category, $tag = null, $not_limit = false)
    { 
        $tag_id = null;
        if ($tag) {
            $tag_id = $tag->id;    
        }
           
        if ($tags = Cache::instance()->get('category_tags_'.$category->id.'_'.$tag_id.'_'.$not_limit)) {
            return $tags;
        }
        
        $offset = 0;
        $count  = self::get_tags_per_page();
        if ($tag) {
            $offset = $category->count_tags - $count;
            if ($offset < 0) {
                $offset = 0;    
            }
            
            mt_srand($tag->id);
            $offset = mt_rand(0, $offset);   
        }
        
        $tags = ORM::factory('tag')
                    ->where('category_id', '=', $category->id)
                    ->where('count_products', '!=', 0);
                    
        if (!$not_limit) {
            $tags->offset($offset)
                 ->limit($count);    
        }
                    
        $tags = $tags
                    //->order_by('count_products', 'DESC')
                    ->find_all()
                    ->as_array();
                    
        Cache::instance()->set('category_tags_'.$category->id.'_'.$tag_id.'_'.$not_limit, $tags);
        return $tags;                                         
    }
    
    public function get_tags_by_product($product)
    {  
        $ids = array();
        $_ids = DB::select()
                    ->from('product_tags')
                    ->where('product_id', '=', $product->id)
                    ->execute()
                    ->as_array();
        foreach ($_ids as $id) {
            $ids[$id['tag_id']] = $id['tag_id'];    
        }
        
        if (!count($ids)) {
            return array();    
        }
          
        $tags = ORM::factory('tag')
                    ->where('id', 'IN', $ids)
                    ->where('count_products', '!=', 0)
                    ->order_by('count_products', 'DESC')
                    ->limit(30)
                    ->find_all()
                    ->as_array();
                    
        return $tags;                                         
    }
    
    public function get_tag_by_url($category, $url)
    {
        $category_id = $category;
        if ($category instanceof Model_Category) {
            $category_id = $category->id;
        }
	
        $tag = ORM::factory('tag')
                    ->where('category_id', '=', $category_id) 
                    ->where('url', '=', $url)
                    ->find();
        /*if (!$tag->loaded()) {
            throw new HTTP_Exception_404('Запись не найдена');      
        }*/
        return $tag;
    }
}